--#include "data\config\circle\CircleConsumes.lua" once	--转生消耗配置
--#include "data\functions\Activity\OpenServer\OpenServerSports.lua" once
--#include "data\functions\Activity\OpenServer\OpenServerPKRank.lua" once    	
--#include "data\functions\welfare\OfflineExpCopy.lua"  once  		--离线副本
--#include "data\functions\welfare\UpgradeGift.lua"  once  		--成长好礼
--#include "data\functions\welfare\LevelUpWelfare.lua"  once  		--等级福利
--#include "data\functions\mail\MailCommon.lua"  once  		--邮件事件


--转生条件检测
function CheckCircleCondition(sysarg, config)

	--local level = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL)
	if not Actor.checkActorLevel(sysarg, config.reqLevel, 0) then
		Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.c000007, config.reqLevel), ttFlyTip)
		return false
	end	

	if config.consumes then
		if not CheckConsumeCond( sysarg, config.consumes, 1 ) then
			--Actor.sendTipmsg(sysarg, Lang.ScriptTips.c000008, ttFlyTip)
			return false
		end
	end
	
	return true
end

--保存曾经到达过的最高等级
function AddOnceLevels( sysarg , nCurrentLevel)
	local var = Actor.getStaticVar( sysarg )
	if not var.OnceLevel then
		var.OnceLevel=0
	end
	if nCurrentLevel>var.OnceLevel then
		var.OnceLevel = nCurrentLevel
	end
end

function CircleHandleImpl(sysarg, nCircle)
	local config = Circle.CircleConsumes[nCircle]
	if not config then
		System.trace("Can not find CircleConsumes config, circle="..nCircle)
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.x00119,ttFlyTip)
		return
	end
	
	--条件检测
	local bCheck = CheckCircleCondition(sysarg, config)
	if not bCheck then
		return
	end
	
	if config.consumes then
		DoConsumeCond( sysarg, config.consumes, 1, 809, Lang.LogTips.l00196 )
	end
	
	local currentLevel = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL)
	local myLevel = currentLevel - config.chgLevel
	--转生
	Actor.setActorCircle(sysarg, nCircle, myLevel)
		
	local msg = string.format(Lang.ScriptTips.c000009, nCircle)
	Actor.sendTipmsg(sysarg, msg, ttFlyTip)
   
	AddOnceLevels(sysarg, currentLevel)								--保存总共掉落的级别
	OpenServerTriggleEvent(sysarg, enOpenServerId_Leveling, myLevel, nCircle)   --开服活动等级竞技
	OpenServerPKRankTriggleEvent(sysarg, enOpenServerRank_Leveling, myLevel, nCircle)
	OnMailEvent( sysarg, nCircle, myLevel)									--邮件事件
	UpgradeGiftLevelChange(sysarg, nCircle, myLevel)				--成长好礼
	LevelUpWelfareLevelChange(sysarg, nCircle, myLevel) 			--等级福利
	OfflineExpActorLevelChange(sysarg)								--离线副本经验改变
	Actor.triggerQuestEvent(sysarg, 5, 0, myLevel, false)			--任务
	Actor.triggerAchieveEvent(sysarg, 78, 1, nCircle)				--成就
	OnCarnivalOpenServerRankTriggerEvent(sysarg, Carnival_Rank_Type_Circle_1, nCircle)
end
